//专题分类信息.js
document.ready=function(){
	if(index==0){
		createAddItemHTML();
	}
}
function createAddItemDiv(){
    //查询当前分类是否保存
    console.log(index);
    var itemId = $("#itemId" + index).val();
    //分类ID为空时表示当前分类没有保存
    if(itemId == null || itemId == ""){
        layer.alert("请先保存当前分类");
        return false;
    }
    //页面上的分类数据和数据库中数据进行比对，判断分类信息是否已经修改
    var templateSpecialTopicId = $("#templateSpecialTopicId").val();
    layer.closeAll('dialog');
    var layer_load_window = layer.load(2); 
    $.ajax({
        url : path + "/special_topic/query_template_special_topic_item_is_change",
        cache : false,
        type : 'post',
        dataType : 'text',
        data : $("#createForm" + index).serialize() + "&templateSpecialTopicId=" + templateSpecialTopicId ,
        error : function() {
        },
        success : function(data) {
            if(data == "true"){
                layer.confirm("您要先保存当前分类再新增专题分类吗",function(){
                	layer.closeAll('dialog');
                },function(){
                	layer.closeAll('dialog');
                    createAddItemHTML();
                });
            }else{
                createAddItemHTML();
            }
       },complete: function(XMLHttpRequest, textStatus) {
    		layer.close(layer_load_window);
       }
    });
}

function createAddItemHTML(num){
	if(num!=null&&num==1){
		index=0;
	}
    index++;
    var html = document.getElementById("itemContent").innerHTML;
    html = html.replace("createForm","createForm"+index);
    html = html.replace("itemName","itemName" + index);
    html = html.replace("itemDescription","itemDescription" + index);
    html = html.replace("selectProduct()","selectProduct(" + index + ")");
    html = html.replace("productIds","productIds" + index);
    html = html.replace("addItem()","addItem(" + index + ")");
    html = html.replace("deleteItem()","deleteItem(" + index + ")");
    html = html.replace("productTable","productTable" + index);
    html = html.replace("itemId","itemId" + index);
    $("#itemListDiv").append(html);
    new jscolor(document.getElementsByClassName("jscolor")[index]);
}
function addItem(index){
	if(index==null|| index == ""){
		return;
	}
    var itemName = $("#itemName"+index).val();
    if(itemName == null || itemName == ""){
      layer.alert("请提供专题分类名称");
        return false;
    }
    if(itemName.length > 8){
      layer.alert("专题分类名称最多不超过8个字");
        return false;
    }
    var productIds = $("#productIds"+index).val();
    if(productIds == null || productIds == ""){
      layer.alert("请选择分类商品");
        return false;
    }
    var templateSpecialTopicId = $("#templateSpecialTopicId").val(); 
    layer.closeAll('dialog');
    var layer_load_window = layer.load(2); 
    $.ajax({
        url : path + "/special_topic/create_template_special_topic_item",
        cache : false,
        type : 'post',
        dataType : 'text',
        data : $("#createForm" + index).serialize() + "&templateSpecialTopicId=" + templateSpecialTopicId,
        error : function() {
        },
        success : function(data) {
            var dataArr = data.split(",");
            if(dataArr[0] == "success"){
              layer.alert("保存专题分类成功");
                $("#itemId" + index).val(dataArr[1]);
            }else{
              layer.alert(dataArr[1]);
            }
        },complete: function(XMLHttpRequest, textStatus) {
        	layer.close(layer_load_window);
        }
    });
}
function selectProduct(index_num){
	var perHtml = $("#productTable" + index_num).html();
	var perProductIds = $("#productIds" + index_num).val();
	if(document.getElementById("hiddenDiv" + index_num) == null) {
		$(document.body).append("<div id='hiddenDiv" + index_num + "' style='display:none;height:0px;width:0px;'></div>");
	}
	if(document.getElementById("productIds" + index_num) == null) {
		$(document.body).append("<input id='hiddenProductIds" + index_num + "' type='hidden' value=''/>");
	}
	
	$("#hiddenProductIds" + index_num).val(perProductIds)
	$("#hiddenDiv" +index_num).text(perHtml);
	var url=path+"/special_topic/query_product_list?index="+index_num;
    var index = layer.open({
        type: 2,
        title: "添加到专题分类商品列表",
        scrollbar: false,
        content: url,
        cancel: function(){
        	$("#productTable" + index_num).html($("#hiddenDiv" +index_num).text());
        	$("#productIds" + index_num).val(perProductIds);
        	//showSelectedProduct(index_num);
    	}   
    });
    layer.full(index);
}
//删除分类表格中选中商品
function deleteSelectedProduct(productId,index){
	layer.confirm("确定删除商品吗？",function(){
        deleteProduct(productId,index);
        var tr = document.getElementById(index + "tr" + productId);
        var rowIndex = tr.rowIndex;
        document.getElementById("productTable" + index).deleteRow(rowIndex);
        layer.closeAll('dialog');
	});
}

//删除商品ID
function deleteProduct(productId,index){
    var productIds = document.getElementById("productIds" + index).value;
    var productIdArr =     productIds.split(",");
    var result = "";
    for(var i=0;i<productIdArr.length;i++){
        if(productIdArr[i] != productId){
            if(result.length == 0){
                result = result + productIdArr[i];
            }else{
                result = result + "," + productIdArr[i];
            }
        }
    }
    document.getElementById("productIds" + index).value = result;
}
//添加选中的商品ID
function addProduct(productId,index){
    var productIds = document.getElementById("productIds" + index).value;
    var productIdArr =     productIds.split(",");
    var exist = false;
    for(var i=0;i<productIdArr.length;i++){
        if(productIdArr[i] == productId){
            exist = true;
            break;
        }
    }
    if(!exist){
        if(productIds.length == 0){
            productIds = productIds + productId;
        }else{
            productIds = productIds + "," + productId;
        }
    }
    document.getElementById("productIds" + index).value = productIds;
}


function showSelectedProduct(index){
    //清空表格中原来选择的商品
    var len = document.getElementById("productTable" + index).rows.length;
    for(var i=1;i<len;i++){
        document.getElementById("productTable" + index).deleteRow(1);
    }
    var productIds = document.getElementById("productIds" + index).value;
    if(productIds.length != null){
    	layer.closeAll('dialog');
    	var layer_load_window = layer.load(2); 
         $.ajax({
                url : path + "/special_topic/query_selected_product_detail",
                cache : false,
                type : 'post',
                dataType : 'json',
                data :{"productIds":productIds},
                error : function() {
                },
                success : function(data) {
                    var productList = eval(data);
                    for(var i=0;i<productList.length;i++){
                        //商品分类
                        var prodTypeName = "";
                        if(productList[i].productProperty != null){
                            if(productList[i].productProperty.prodTypeFour!=null){
                                prodTypeName = productList[i].productProperty.prodTypeFour.name;
                            }else if(productList[i].productProperty.prodTypeThree!=null){
                                prodTypeName = productList[i].productProperty.prodTypeThree.name;
                            }else if(productList[i].productProperty.prodTypeTwo!=null){
                                prodTypeName = productList[i].productProperty.prodTypeTwo.name;
                            }else if(productList[i].productProperty.prodTypeOne!=null){
                                prodTypeName = productList[i].productProperty.prodTypeOne.name;
                            }
                        }
                        //商品原价
                        var payCostMileage = "";
                        if(productList[i].priceInfo.payCostMileage != null){
                            payCostMileage = productList[i].priceInfo.payCostMileage + "公里";
                        }
                        //商品价格
                        var payOneMileage = "";
                        if(productList[i].priceInfo.payOneMileage != null){
                            payOneMileage = productList[i].priceInfo.payOneMileage + "公里";
                        }
                        //商品名称
                        var productName = "";
                        if(productList[i].baseInfo.name != null){
                            productName  = productList[i].baseInfo.name;
                        }
                        //商品库存
                        var storeNum = "";
                        if(productList[i].storeInfo.storeNum != null){
                            storeNum = productList[i].storeInfo.storeNum;
                        }
                        $("#productTable" + index).append("<tr  class='text-c' id="  + index + "tr" + productList[i].id + ">" +
                                 "<td>" + productList[i].id + "</td>"+
                                 "<td>" + productName + "</td>"+
                                 "<td>" + prodTypeName + "</td>"+
                                 "<td>" + productList[i].merchant.mercName + "</td>"+
                                 "<td>" + storeNum + "</td>"+
                                 "<td>" + payCostMileage + "</td>"+
                                 "<td>" + payOneMileage + "</td>"+
                                 "<td><a onclick='deleteSelectedProduct(" +productList[i].id + "," + index + ")'>删除</a></td>"+
                         "</tr>");
                    }
                },complete: function(XMLHttpRequest, textStatus) {
                	layer.close(layer_load_window);
                }
              });
    }
}

function deleteItem(num){
    var itemId = $("#itemId" + num).val();
    layer.confirm("确定删除当前分类？",function(){
        if(itemId != null && itemId != ""){
        	layer.closeAll('dialog');
        	var layer_load_window = layer.load(2); 
            $.ajax({
                url : path + "/special_topic/delete_template_special_topic_item",
                cache : false,
                type : 'post',
                dataType : 'text',
                data : {"templateSpecialTopicItemId":itemId},
                error : function() {
                },
                success : function(data) {
                    var dateArr = data.split(",");
                    if(dateArr[0] == "success"){
                        $("#createForm" + index).parent().remove();
                        var formNum = $("form[name='createForm']").length;
                        if(formNum == 1){
                            createAddItemHTML(1);
                        }
                        layer.msg(dateArr[1],{icon:1,time:1000});
                    }else{
                        layer.alert(dateArr[1]);
                    }
                 },complete: function(XMLHttpRequest, textStatus) {
                		layer.close(layer_load_window);
                 }
              });
        }else{
            $("#createForm" + index).parent().remove();
            index--;
            var formNum = $("form[name='createForm']").length;
            if(formNum == 1){
                createAddItemHTML(1);
            }
        	layer.closeAll('dialog');
        }
    });
}
function returnList(){
	var index = parent.layer.getFrameIndex(window.name);
	parent.$("#search_form").submit();
	parent.layer.close(index);
}